<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" @tab-click="handleTabChange">
      <el-tab-pane label="所有租户" :name="0" />
      <el-tab-pane label="正常租户" :name="1" />
      <el-tab-pane label="欠费租户" :name="2" />
      <el-tab-pane label="欠费已停电" :name="3" />
      <el-tab-pane label="欠费未停电" :name="4" />
    </el-tabs>
    <el-form :model="queryParams" :inline="true" style="margin-bottom: 16px">
      <el-form-item label="企业名称" prop="qymc">
        <el-input v-model="queryParams.qymc" placeholder="请输入企业名称" style="width: 200px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tenantList" border v-loading="loading">
      <el-table-column prop="zt" label="载体" align="center" />
      <el-table-column prop="qymc" label="企业名称" align="center" />
      <el-table-column prop="khbh" label="客户编号" align="center" />
      <el-table-column prop="jflx" label="计费类型" align="center">
        <template #default="scope">
          <span v-if="scope.row.jflx === 1">后付费</span>
          <span v-else-if="scope.row.jflx === 2">预充值</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column prop="sfkdd" label="可停电" align="center">
        <template #default="scope">
          <span v-if="scope.row.sfkdd === 1">可停电</span>
          <span v-else-if="scope.row.sfkdd === 2">不可停电</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column prop="sfdd" label="停电状态" align="center">
        <template #default="scope">
          <span v-if="scope.row.sfdd === 1">未停电</span>
          <span v-else-if="scope.row.sfdd === 2">已停电</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column prop="wfksjec" label="待核销电费(元)" align="center" />
      <el-table-column prop="csyje" label="可用余额(元)" align="center" />
      <el-table-column prop="dbf" label="当月实时电费(元)" align="center" />
      <el-table-column prop="cyje" label="剩余余额(元)" align="center" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="fetchTenantList"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { selectBillingTenantStatus } from '@/api/bill/index';

const activeTab = ref('0');
const loading = ref(false);
const tenantList = ref([]);
const total = ref(0);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  qymc: '',
  status: 0
});

function fetchTenantList() {
  loading.value = true;
  selectBillingTenantStatus({ ...queryParams }).then(res => {
    tenantList.value = res.data?.list || [];
    total.value = res.data?.total || 0;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

function handleTabChange(tab) {
    console.log(tab.props.name);
  queryParams.status = Number(tab.props.name);
  queryParams.pageNum = 1;
  fetchTenantList();
}

function handleQuery() {
  queryParams.pageNum = 1;
  fetchTenantList();
}

function resetQuery() {
  queryParams.qymc = '';
  queryParams.pageNum = 1;
  fetchTenantList();
}

onMounted(() => {
  fetchTenantList();
});
</script>
